#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>


int main()
{
    pid_t id = fork();
    if(id == 0)
    {
        //子进程
        int cnt = 5;
        while(cnt)
        {
            printf("I  am child process. I have %d seconds to exit. pid: %d, ppid: %d\n", cnt--, getpid(), getppid());
            sleep(1);
            //bint* p = NULL;
           //p = 100;
        }
        if(1) exit(114);
    }
    // 父进程
    while(1)
    {
        int status = 0;
        pid_t ret_id = waitpid(id, &status, WNOHANG); // 夯住了
        if(ret_id < 0)
        {
            printf("waitpid error!\n");
            exit(1);
        }
        else if(ret_id == 0)
        {
            printf("I am father process, I have other to do....\n");
            sleep(1);
            continue;
        }
        else{
            printf("I am father process. Wait sucess!!! pid: %d, ppid: %d, ret_id: %d, child exit code: %d, child exit signal: %d\n",\
                getpid(), getppid(), ret_id, (status>>8)&0xFF, status & 0x7F);
            break;
        }
    }
}
